package com.xyhuo.leetcode.y2025;

public class CountSymmetricIntegers {
    public int countSymmetricIntegers(int low, int high) {
        int ans = 0;
        for (int i = low; i <= high; i++) {
            if (isSymmetric(i)) {
                ans++;
            }
        }
        return ans;
    }

    private boolean isSymmetric(int num) {
        String str = String.valueOf(num);
        int length = str.length();
        if (length % 2 != 0) {
            return false;
        }
        char[] charArray = str.toCharArray();
        int first = 0, second = 0;
        int middle = length / 2;
        for (int i = 0; i < charArray.length; i++) {
            if (i < middle) {
                first += Integer.valueOf(charArray[i]);
            } else {
                second += Integer.valueOf(charArray[i]);
            }
        }
        return first == second;
    }
}
